package com.dhcc.controller;

import com.dhcc.api.R;
import com.dhcc.entity.*;
import com.dhcc.service.*;
import com.dhcc.utils.AuthUtil;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @ProjectName Dhcc-Cloud
 * @PackageName com.dhcc.controller
 * @Title AuthController
 * @Date 2024/10/11 下午3:46
 * @Author LiuGuoting
 */
@RestController
@RequestMapping("/userSystem")
public class SystemController {
    @Resource
    private SysInstitutionInstitutionRoleService sysInstitutionInstitutionRoleService;
    @Resource
    private SysInstitutionRoleService institutionRoleService;
    @Resource
    private SysUserDepartmentInstitutionService sysUserDepartmentInstitutionService;
    @Resource
    private SysUserPositionService sysPositionService;
    @Resource
    private SysOrganizationProductService sysOrganizationProductService;
    @Resource
    private SysFinanceService sysFinanceService;
    @Resource
    private SysUserRoleInstitutionRoleFinanceService sysUserRoleInstitutionRoleFinanceService;
    @Resource
    private SysUserResourceService sysResourceService;
    @Resource
    private SysUsersService sysUserService;
    @Resource
    private SysUserOrganizationService sysUserOrganizationService;
    @Resource
    private SysInstitutionService sysInstitutionService;
    @Resource
    private SysDepartmentService sysDepartmentService;
    @Resource
    private ClientUserService clientUserService;

    /**
     * 查询当前机构机构角色列表
     */
    @GetMapping("/getInstitutionRoles")
    public R getInstitutionRoles(String institutionId){
        String institutionRoleId = sysInstitutionInstitutionRoleService.getInstitutionRoles(institutionId);
        //Map<String,Object> institutionRoleMap=new HashMap<>();
        List<SysInstitutionRole> sysInstitutionRoleList=new ArrayList<>();
        SysInstitutionRole institutionRole = institutionRoleService.getInstitutionRole(institutionRoleId);
        sysInstitutionRoleList.add(institutionRole);
        return R.data(sysInstitutionRoleList);
    }

    /**
     * 通过用户id，机构编号,机构角色编号查询当前用户的部门
     */
    @GetMapping("/getDepartment")
    public R getDepartment(String institutionId,String institutionRoleId){
        List<String> departmentIds=sysUserDepartmentInstitutionService.getDepartment(institutionId,AuthUtil.getUserId(),institutionRoleId);
        List<SysDepartment> sysDepartmentList=new ArrayList<>();
        //Map<String,String> departmentMap=new HashMap();
        for (String departmentId : departmentIds) {
            SysDepartment sysDepartment=sysDepartmentService.getDepatment(departmentId);
            sysDepartmentList.add(sysDepartment);
        }
        return R.data(sysDepartmentList);
    }
    /**
     * 根据用户编号，机构编号，部门编号查询部门下的岗位信息
     */
    @GetMapping("/getPosition")
    public R getPosition(String institutionId, String departmentId,String institutionRoleId){
        List<String> positionIds=sysUserDepartmentInstitutionService.getPositionIds(institutionId,departmentId,AuthUtil.getUserId(),institutionRoleId);
        //Map<String,String> positionMap=new HashMap<>();
        List<SysUserPosition> sysUserPositionList =new ArrayList<>();
        for (String positionId : positionIds) {
            SysUserPosition sysPosition=sysPositionService.getPositionName(positionId);
            sysUserPositionList.add(sysPosition);
        }
        return R.data(sysUserPositionList);

    }
    /**
     * 根据机构角色id,机构id，部门id，岗位id查询对应的产品列表
     */
    @GetMapping("/getFinanceList")
    public R getFinanceList(String institutionId,String institutionRoleId,String departmentId,String positionId ){
        //List<String> productIdList= sysOrganizationProductService.getProductList(institutionId);
        List<String> productIdList=sysUserRoleInstitutionRoleFinanceService.getproductId(institutionId,institutionRoleId,departmentId,positionId);
        List<SysFinance> productList=new ArrayList<>();
        for (String productId : productIdList) {
            SysFinance sysFinance=sysFinanceService.getFinanceById(productId);
            productList.add(sysFinance);
        }
        return R.data(productList);
    }
    /**
     * 通过机构角色id，部门编号，产品编号查询资源列表
     */
    @GetMapping("/getResourceList")
    public R getResourceList(String institutionRoleId,String positionId ,String financeId){
        List<String> resourceIdList= sysUserRoleInstitutionRoleFinanceService.getResourceIdList(institutionRoleId,positionId,financeId);
        List<SysUserResource> resourceList=new ArrayList<>();
        for (String resourceId : resourceIdList) {
            SysUserResource sysResource=sysResourceService.getResourceList(resourceId);
            resourceList.add(sysResource);
        }
        return R.data(resourceList);
    }
    /**
     * 查询当前用户的用户信息
     */
    @GetMapping("/getUserInfo")
    public R<ClientUser> getUserInfo(){
        return R.data(clientUserService.getUserById(AuthUtil.getUserId()));
    }
    /**
     * 查询当前用户所在机构列表
     */
    @PostMapping("/getInstitutionList")
    public R getInstitutionList(){
        List<String> institutionIdList = sysUserOrganizationService.selectInstitutionIdList(AuthUtil.getUserId());
        //Map<String,Object> institutionMap=new HashMap<>();
        List<SysInstitution> sysInstitutionList=new ArrayList<>();
        for (String institutionId : institutionIdList) {
            SysInstitution sysInstitution = sysInstitutionService.selectInstitutionById(institutionId);
            sysInstitutionList.add(sysInstitution);
        }
        return R.data(sysInstitutionList);
    }


}
